package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.constant.Constant;
import com.demo.entity.DvtDiagnose;
import com.demo.entity.DvtWellsScoreItem;
import com.demo.mapper.DvtDiagnoseMapper;
import com.demo.mapper.DvtWellsScoreItemMapper;
import com.demo.response.Result;
import com.demo.service.IDvtWellsScoreItemService;
import com.demo.util.TimeUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * dvt风险wells项目 服务实现类
 *
 * @author zm
 * @since 2020-11-25
 */
@Service
public class DvtWellsScoreItemServiceImpl extends ServiceImpl<DvtWellsScoreItemMapper, DvtWellsScoreItem> implements IDvtWellsScoreItemService {
    @Resource
    DvtWellsScoreItemMapper dvtWellsScoreItemMapper;
    @Resource
    DvtDiagnoseMapper dvtDiagnoseMapper;

    /**
     * 新增dvt风险wells项目
     *
     * @param dvtWellsScoreItem 实体对象
     * @return
     */
    @Override
    public Result addDvtWellsScore(DvtWellsScoreItem dvtWellsScoreItem) {
        boolean flag = this.save(dvtWellsScoreItem);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 编辑dvt风险wells项目
     *
     * @param dvtWellsScoreItem 实体对象
     * @return
     */
    @Override
    public Result updateDvtWellsScore(DvtWellsScoreItem dvtWellsScoreItem) {
        if (dvtWellsScoreItem.getDvtItemId() == null || dvtWellsScoreItem.getUpdatedUserId() == null) {
            return Result.error(Constant.KEY_ID);
        }
        boolean flag = this.updateById(dvtWellsScoreItem);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 删除dvt风险wells项目
     *
     * @param dvtItemId     主键ID
     * @param deletedUserId 删除人ID
     * @return
     */
    @Override
    public Result deleteDvtWellsScore(Integer dvtItemId, Integer deletedUserId) {
        DvtWellsScoreItem item = new DvtWellsScoreItem();
        item.setDvtItemId(dvtItemId);
        item.setDeletedUserId(deletedUserId);
        item.setDeletedTime(new Date());
        boolean flag = this.updateById(item);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * dvt风险wells项目列表
     *
     * @param pageNumber  页码
     * @param pageSize    每页数量
     * @param dvtItemName dvt风险wells项目名称
     * @param dvtItemMode 评估模型：1Caprini   2Padua
     * @return
     */
    @Override
    public Result dvtWellsScoreList(Integer pageNumber, Integer pageSize, String dvtItemName, Integer dvtItemMode, Integer dvtDiagnoseId, Boolean allItem) {
        IPage<DvtWellsScoreItem> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<DvtWellsScoreItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(TimeUtil.isString(dvtItemName), "dvt_item_name", dvtItemName);
        queryWrapper.eq(dvtItemMode != null, "dvt_item_mode", dvtItemMode);
        queryWrapper.orderByAsc("dvt_item_sort");
        if (allItem) {
            DvtDiagnose diagnose = dvtDiagnoseMapper.selectById(dvtDiagnoseId);
            // 当前dvt评估记录医生尚未签字则只查询未删除的
            if (diagnose != null && diagnose.getSignTime() == null) {
                queryWrapper.isNull("deleted_user_id");
            } else if (diagnose != null && diagnose.getSignTime() != null) {
                // 当前dvt评估记录医生已签字则签字时间前就已经被删除的项就过滤掉
                queryWrapper.and(wrapper -> wrapper.gt("deleted_time", diagnose.getSignTime()).or().isNull("deleted_time"));
            }
        } else {
            queryWrapper.isNull("deleted_time");
        }
        page = dvtWellsScoreItemMapper.selectPage(page, queryWrapper);
        PageInfo<DvtWellsScoreItem> pageInfo = new PageInfo<>(page.getRecords());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPages((int) page.getPages());
        if (pageNumber.equals(page.getPages())) {
            pageInfo.setIsLastPage(false);
        } else {
            pageInfo.setIsLastPage(true);
        }
        return Result.success(pageInfo);
    }
}
